using System.Xml.Linq;

namespace DeclarativeTest.Core.Support
{
	public static class ParserExtensions
	{
		/// <summary>
		/// Parses test suite from text.
		/// </summary>
		/// <param name="parser">Parser</param>
		/// <param name="text">String in a correct format</param>
		/// <returns>An instance of <see cref="TestSuiteInfo"/></returns>
		public static TestSuiteInfo ParseFromString( this ITestSuiteParser parser, string text )
		{
			// Check arguments
			AssertUtils.ArgumentIsNullOrEmpty( text, "text", "Can not parse from null or empty string" );

			// XDocument parsing
			XDocument document = XDocument.Parse( text );

			// Parse from document
			return parser.ParseTestSuite( document );
		}


		/// <summary>
		/// Parses test suite from file.
		/// </summary>
		/// <param name="parser">Parser</param>
		/// <param name="fileName">File name to load and parse</param>
		/// <returns>An instance of <see cref="TestSuiteInfo"/></returns>
		public static TestSuiteInfo ParseFromFile( this ITestSuiteParser parser, string fileName )
		{
			// Check arguments
			AssertUtils.AssertFileExists( fileName, "fileName" );
			
			// XDocument loading
			XDocument document = XDocument.Load( fileName );

			// Parse from document
			return parser.ParseTestSuite( document );
		}

	}
}